package com.ruoyi.web.controller.chapterapply;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.BorrowingAmount;
import com.ruoyi.domain.ChapterApply;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.service.IChapterApplyService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/ruoyi/applymanager")
public class ChapterApplyManagerController extends BaseController {
    private String prefix = "ruoyi/apply";

    @Autowired
    private IChapterApplyService chapterApplyService;

    @RequiresPermissions("ruoyi:applymanager:view")
    @GetMapping()
    public String apply()
    {
        return prefix + "/applyManager";
    }

    /**
     * 查询用章申请列表
     */
    @RequiresPermissions("ruoyi:apply:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ChapterApply chapterApply)
    {
        startPage();
        List<ChapterApply> list = chapterApplyService.selectChapterApplyList(chapterApply);
        list.removeIf(b -> b.getcStatus() == 0);
        return getDataTable(list);
    }

    /**
     * 导出用章申请列表
     */
    @RequiresPermissions("ruoyi:apply:export")
    @Log(title = "用章申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ChapterApply chapterApply)
    {
        List<ChapterApply> list = chapterApplyService.selectChapterApplyList(chapterApply);
        ExcelUtil<ChapterApply> util = new ExcelUtil<ChapterApply>(ChapterApply.class);
        return util.exportExcel(list, "apply");
    }

    /**
     * 新增用章申请
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用章申请
     */
    @RequiresPermissions("ruoyi:apply:add")
    @Log(title = "用章申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ChapterApply chapterApply)
    {
        chapterApply.setcPeople(ShiroUtils.getLoginName());
        return toAjax(chapterApplyService.insertChapterApply(chapterApply));
    }

    /**
     * 修改用章申请
     */
    @GetMapping("/edit/{cId}")
    public String edit(@PathVariable("cId") Long cId, ModelMap mmap)
    {
        chapterApplyService.editparoStatus(cId);
        ChapterApply chapterApply = chapterApplyService.selectChapterApplyById(cId);
        mmap.put("chapterApply", chapterApply);
        return prefix + "/applyVerifyedit";
    }

    /**
     * 修改保存用章申请
     */
    @RequiresPermissions("ruoyi:apply:edit")
    @Log(title = "用章申请", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ChapterApply chapterApply)
    {
        return toAjax(chapterApplyService.updateChapterApply(chapterApply));
    }

    /**
     * 删除用章申请
     */
    @RequiresPermissions("ruoyi:apply:remove")
    @Log(title = "用章申请", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(chapterApplyService.deleteChapterApplyByIds(ids));
    }

    /**
     * 总经理通过申请
     */
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping( "/consent")
    @ResponseBody
    public AjaxResult consent(Long cId) {
        chapterApplyService.conExaStatus(cId);
        return success();
    }
}
